#!/usr/bin/Python38

# import math
import numpy as np
import matplotlib.pyplot as plt


def on_press(event):
    if event.inaxes is None:
        return
    for line in event.inaxes.lines:
        if event.key == 't':
            visible = line.get_visible()
            line.set_visible(not visible)
    event.inaxes.figure.canvas.draw()


def LearnPLT():
    """
    学习matplotlib库
    """
    fig, ax = plt.subplots(1)
    fig.canvas.mpl_connect('key_press_event', on_press)
    ax.plot(np.random.rand(2, 20))
    plt.show()


def LearnNumpy():
    """
    学习Numpy库
    """
    np_arr = np.array([[[1, 2]], [[3, 4]], [[5, 6]], [[7, 8]], [[9, 10]], [[11, 12]]])
    A = np.array([range(5)], dtype=np.long, ndmin=2)
    print(A)
    B = np.delete(np_arr, 2, axis=0)  # 此函数处理完之后对原数组没有改变
    print("after delete!")
    print(B)
    print("the first print is over!")
    np_arr = np_arr[:, np.newaxis, :]
    # print(np_arr)
    # 定义结构化数据类型,类似于C语言中的结构体
    dt = np.dtype([('x', np.float), ("y", np.float), ('z', np.float)])
    C = np.array([(10, 11, 12), (20, 21, 22), (30, 31, 32)], dtype=dt)
    print(C[0]['y'])


def LearnWhileLoop():
    """
    学习while循环
    """
    print("数字猜谜游戏")

    numC = 7
    numD = -1

    print("Guess the number!")

    while numD != numC:
        numD = int(input("is it ..."))
        print("the number of input is : ", numD)
        if numD == numC:
            print("Hooray! you guessed it right!")
        elif numD < numC:
            print("It's bigger...")
        else:
            print("It's not so big.")
    return


def LearnForLoop():
    """
    学习for循环
    """
    edibles = ["ham", "spam", "eggs", "nuts"]

    for food in edibles:
        if food == "spam":
            print("No more spam please!")
            break
        print("Great, delicious " + food)
    # 此处的else是for循环的
    else:
        print("I am so glad: No spam!")
    print("Finally, I finished stuffing myself")
    return


def LearnIter():
    """
    迭代器学习
    """

    aList = range(10)  # 使用0到9的10个数初始化列表
    bList = [0 for i in range(10)]  # 使用10个0初始化列表
    cList = [0] * 10  # 初始化每项为0的固定长度数组
    dList = [[0 for col in range(5)] for row in range(6)]  # 使用固定值列表初始化2维数组列表
    eList = [[0] * 10 for row in range(6)]  # 相当于[cList for i in range(6)]
    eList[0][0] = 2    # 只修改数组第一行第一列的元素值
    fList = [[0]*3] * 5    # 类似于创建cList的方法创建多维数组列表
    fList[0][0] = 3    # 修改每一行第一个元素的值(注意和eList的不同之处)
    print(eList)
    listIter = iter(dList)
    for x in listIter:
        print(x, end=" ")
    else:
        print("...")

    return


def main():
    """
    调用不同的函数进行测试
    """
    # LearnNumpy()
    LearnPLT()
    return


if __name__ == '__main__':
    main()
